<?php
namespace Api\Controller;
use Think\Controller;
class PayController extends CommonController {
	public function tjorder(){
		$addr_id = I( "get.addr_id");
		$sum = I ("get.sum");
		$Shopcart = M ("Shopcart");
		$d =array(
			'status' =>1
		);
		$ret = $Shopcart->where("user_id={$_SESSION['user_id']} and selected = 1")->setField($d);
		if($ret == false){
			$this->response(false,0,"未知错误",true);
			exit;	
		}else{	
			$Order = M ( "Order");
			$order_no =array(
				'order_no' =>date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8)
			);	
			$Order_res = $Shopcart->where("user_id={$_SESSION['user_id']} and status = 1 and order_no = 0")->setField($order_no);
			if($Order_res == false){
				$this->response(false,0,"未知错误",true);
				exit;	
			}else{
				$data = array(
					'addr_id' =>$addr_id,
					'user_id' =>$_SESSION['user_id'],
					'order_no' =>$order_no['order_no'],
					'sum' =>$sum,
					'addtime' =>time()
				);
				
				$Order_ret = $Order->add($data);
				//echo $Order->getLastSql();
				if($Order_ret == false){
					$this->response(false,0,"未知错误",true);
				}else{
					$this->response(true,1,$data['order_no'],true);
				}
			}	
		}
	}

}
